﻿namespace Mp3Observer.Common
{
    #region usings

    using System;
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    #endregion

    /// <summary>
    /// Helper class for xml serializing/deserializing
    /// </summary>
    public static class Serializer
    {
        /// <summary>
        /// Deserializes the specified stringreader
        /// </summary>
        /// <typeparam name="T">generic type of the xml doc</typeparam>
        /// <param name="reader">The stringreader.</param>
        /// <returns>a new instance of type <typeparamref name="T"/></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "generic methods are ok")]
        public static T Deserialize<T>(TextReader reader) where T : new()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(reader);
        }

        /// <summary>
        /// Deserializes the specified file name.
        /// </summary>
        /// <typeparam name="T">generic type of the xml doc</typeparam>
        /// <param name="fileName">Name of the file.</param>
        /// <exception cref="ArgumentException">if the file does not exist</exception>
        /// <returns>a new instance of type <typeparamref name="T"/></returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "generic methods are ok")]
        public static T Deserialize<T>(string fileName) where T : new()
        {
            XmlReader xmlReader = null;
            if (File.Exists(fileName))
                xmlReader = XmlReader.Create(fileName);
            else
                throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "File {0} does not exist.", fileName)); // TODO : Error aus resource

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            T data = (T)serializer.Deserialize(xmlReader);
            xmlReader.Close();
            return data;
        }

        /// <summary>
        /// Serializes the data to a string
        /// </summary>
        /// <typeparam name="T">type of the data</typeparam>
        /// <param name="data">The data(instance of an object) to serialize</param>
        /// <returns>the written string</returns>
        /// <exception cref="XenonException">if any exception is raised (fileaccess, xmlserializer)</exception>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "generic methods are ok")]
        public static string Serialize<T>(object data)
        {
            // StringWriter writer = null;
            try
            {
                using (StringWriter writer = new StringWriter(CultureInfo.CurrentCulture))
                {
                    new XmlSerializer(typeof(T)).Serialize(writer, data);
                    string data2 = writer.ToString();
                    writer.Close();
                    return data2;
                }
            }
            catch (Exception)
            {
                // TODO : welche exception catchen??
                throw;
            }
            // finally
            // {
            //    //if (writer != null)
            //    //    writer.Close();
            // }
            // return writer.ToString();
        }

        /// <summary>
        /// Serializes the specified file name.
        /// </summary>
        /// <typeparam name="T">type of the data</typeparam>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="data">The data(instance of an object) to serialize</param>
        /// <exception cref="XenonException">if any exception is raised (fileaccess, xmlserializer)</exception>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "generic methods are ok")]
        public static void Serialize<T>(string fileName, object data)
        {
            // XmlWriter writer = null;
            try
            {
                using (Stream stream = new FileStream(fileName, FileMode.Create))
                {
                    using (XmlWriter writer = new XmlTextWriter(stream, Encoding.Unicode))
                    {
                        new XmlSerializer(typeof(T)).Serialize(writer, data);
                        writer.Close();
                    }
                    stream.Close();
                }
            }
            catch (Exception)
            {
                // TODO : welche exception catchen??
                throw;
            }
            // finally
            // {
            //    if (writer != null)
            //        writer.Close();
            // }
        }
    }
}
